home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / dflat_r_.arc / FILEOPEN.C < prev    next >
Text File  |  1991-10-02  |  5KB  |  238 lines

  1. /* ----------- fileopen.c ------------- */
  2.  
  3. #include <string.h>
  4. #if MSC | WATCOM
  5. #include <direct.h>
  6. #else
  7. #include <dir.h>
  8. #endif
  9. #include <dos.h>
  10. #include <ctype.h>
  11. #include "dflat.h"
  12.  
  13. #ifdef INCLUDE_DIALOG_BOXES
  14.  
  15. static int DlgFileOpen(char *, char *, DBOX *);
  16. static int DlgFnOpen(WINDOW, MESSAGE, PARAM, PARAM);
  17. static void InitDlgBox(WINDOW);
  18. static void StripPath(char *);
  19. static int IncompleteFilename(char *);
  20.  
  21. static char *OrigSpec;
  22. static char *FileSpec;
  23. static char *FileName;
  24.  
  25. static int Saving;
  26. extern DBOX FileOpen;
  27. extern DBOX SaveAs;
  28.  
  29. /*
  30.  * Dialog Box to select a file to open
  31.  */
  32. int OpenFileDialogBox(char *Fpath, char *Fname)
  33. {
  34.     return DlgFileOpen(Fpath, Fname, &FileOpen);
  35. }
  36.  
  37. /*
  38.  * Dialog Box to select a file to save as
  39.  */
  40. int SaveAsDialogBox(char *Fname)
  41. {
  42.     return DlgFileOpen(NULL, Fname, &SaveAs);
  43. }
  44.  
  45. /* --------- generic file open ---------- */
  46. static int DlgFileOpen(char *Fpath, char *Fname, DBOX *db)
  47. {
  48.     int  rtn;
  49.     char savedir[80];
  50.     char OSpec[80];
  51.     char FSpec[80];
  52.     char FName[80];
  53.  
  54.     OrigSpec = OSpec;
  55.     FileSpec = FSpec;
  56.     FileName = FName;
  57.  
  58.     getcwd(savedir, sizeof savedir);
  59.     if (Fpath != NULL)    {
  60.         strncpy(FileSpec, Fpath, 80);
  61.         Saving = FALSE;
  62.     }
  63.     else    {
  64.         *FileSpec = '\0';
  65.         Saving = TRUE;
  66.     }
  67.     strcpy(FileName, FileSpec);
  68.     strcpy(OrigSpec, FileSpec);
  69.  
  70.     if ((rtn = DialogBox(NULLWND, db, TRUE, DlgFnOpen)) != FALSE)
  71.         strcpy(Fname, FileName);
  72.     else
  73.         *Fname = '\0';
  74.  
  75.     setdisk(toupper(*savedir) - 'A');
  76.     chdir(savedir);
  77.  
  78.     return rtn;
  79. }
  80.  
  81. /*
  82.  *  Process dialog box messages
  83.  */
  84. static int DlgFnOpen(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  85. {
  86.     switch (msg)    {
  87.         case INITIATE_DIALOG:
  88.             InitDlgBox(wnd);
  89.             break;
  90.         case COMMAND:
  91.             switch ((int) p1)    {
  92.                 case ID_FILENAME:
  93.                     if (p2 != ENTERFOCUS)    {
  94.                         /* allow user to modify the file spec */
  95.                         GetItemText(wnd, ID_FILENAME,
  96.                                 FileName, 65);
  97.                         if (IncompleteFilename(FileName) || Saving)    {
  98.                             strcpy(OrigSpec, FileName);
  99.                             StripPath(OrigSpec);
  100.                         }
  101.                         if (p2 != LEAVEFOCUS)
  102.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  103.                     }
  104.                     return TRUE;
  105.                 case ID_OK:
  106.                     GetItemText(wnd, ID_FILENAME,
  107.                             FileName, 65);
  108.                     strcpy(FileSpec, FileName);
  109.                     if (IncompleteFilename(FileName))    {
  110.                         /* no file name yet */
  111.                         InitDlgBox(wnd);
  112.                         strcpy(OrigSpec, FileSpec);
  113.                         return TRUE;
  114.                     }
  115.                     else    {
  116.                         GetItemText(wnd, ID_PATH, FileName, 65);
  117.                         strcat(FileName, FileSpec);
  118.                     }
  119.                     break;
  120.                 case ID_FILES:
  121.                     switch ((int) p2)    {
  122.                         case LB_SELECTION:
  123.                             /* selected a different filename */
  124.                             GetDlgListText(wnd, FileName,
  125.                                         ID_FILES);
  126.                             PutItemText(wnd, ID_FILENAME,
  127.                                             FileName);
  128.                             break;
  129.                         case LB_CHOOSE:
  130.                             /* chose a file name */
  131.                             GetDlgListText(wnd, FileName,
  132.                                     ID_FILES);
  133.                             SendMessage(wnd, COMMAND, ID_OK, 0);
  134.                             break;
  135.                         default:
  136.                             break;
  137.                     }
  138.                     return TRUE;
  139.                 case ID_DRIVE:
  140.                     switch ((int) p2)    {
  141.                         case ENTERFOCUS:
  142.                             if (Saving)
  143.                                 *FileSpec = '\0';
  144.                             break;
  145.                         case LEAVEFOCUS:
  146.                             if (Saving)
  147.                                 strcpy(FileSpec, FileName);
  148.                             break;
  149.                         case LB_SELECTION:    {
  150.                             char dd[25];
  151.                             /* selected different drive/dir */
  152.                             GetDlgListText(wnd, dd,
  153.                                                 ID_DRIVE);
  154.                             if (*(dd+2) == ':')
  155.                                 *(dd+3) = '\0';
  156.                             else
  157.                                 *(dd+strlen(dd)-1) = '\0';
  158.                             strcpy(FileName, dd+1);
  159.                             if (*(dd+2) != ':' && *OrigSpec != '\\')
  160.                                 strcat(FileName, "\\");
  161.                             strcat(FileName, OrigSpec);
  162.                             if (*(FileName+1) != ':' && *FileName != '.')    {
  163.                                 GetItemText(wnd, ID_PATH, FileSpec, 65);
  164.                                 strcat(FileSpec, FileName);
  165.                             }
  166.                             else 
  167.                                 strcpy(FileSpec, FileName);
  168.                             break;
  169.                         }
  170.                         case LB_CHOOSE:
  171.                             /* chose drive/dir */
  172.                             if (Saving)
  173.                                 PutItemText(wnd, ID_FILENAME, "");
  174.                             InitDlgBox(wnd);
  175.                             return TRUE;
  176.                         default:
  177.                             break;
  178.                     }
  179.                     PutItemText(wnd, ID_FILENAME, FileSpec);
  180.                     return TRUE;
  181.  
  182.  
  183.                 default:
  184.                     break;
  185.             }
  186.         default:
  187.             break;
  188.     }
  189.     return DefaultWndProc(wnd, msg, p1, p2);
  190. }
  191.  
  192. /*
  193.  *  Initialize the dialog box
  194.  */
  195. static void InitDlgBox(WINDOW wnd)
  196. {
  197.     if (*FileSpec && !Saving)
  198.         PutItemText(wnd, ID_FILENAME, FileSpec);
  199.     if (DlgDirList(wnd, FileSpec, ID_FILES, ID_PATH, 0))    {
  200.         StripPath(FileSpec);
  201.         DlgDirList(wnd, "*.*", ID_DRIVE, 0, 0xc010);
  202.     }
  203. }
  204.  
  205. /*
  206.  * Strip the drive and path information from a file spec
  207.  */
  208. static void StripPath(char *filespec)
  209. {
  210.     char *cp, *cp1;
  211.  
  212.     cp = strchr(filespec, ':');
  213.     if (cp != NULL)
  214.         cp++;
  215.     else
  216.         cp = filespec;
  217.     while (TRUE)    {
  218.         cp1 = strchr(cp, '\\');
  219.         if (cp1 == NULL)
  220.             break;
  221.         cp = cp1+1;
  222.     }
  223.     strcpy(filespec, cp);
  224. }
  225.  
  226.  
  227. static int IncompleteFilename(char *s)
  228. {
  229.     int lc = strlen(s)-1;
  230.     if (strchr(s, '?') || strchr(s, '*') || !*s)
  231.         return TRUE;
  232.     if (*(s+lc) == ':' || *(s+lc) == '\\')
  233.         return TRUE;
  234.     return FALSE;
  235. }
  236.  
  237. #endif
  238.